package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class AftersaleHandleTypeService{

	@Autowired
	private AftersaleHandleTypeDao aftersaleHandleTypeDao;
	private AftersaleRecordDao aftersaleRecordDao;
	
	
	private QAftersaleHandleType $ = QAftersaleHandleType.aftersaleHandleType;
	private QAftersaleRecord $_ = QAftersaleRecord.aftersaleRecord;
	
	
	@Autowired
	public void setAftersaleHandleTypeDao(AftersaleHandleTypeDao aftersaleHandleTypeDao) {
		this.aftersaleHandleTypeDao = aftersaleHandleTypeDao;
	}
	@Autowired
	public void setAftersaleRecordDao(AftersaleRecordDao aftersaleRecordDao) {
		this.aftersaleRecordDao = aftersaleRecordDao;
	}
	
	public List<AftersaleHandleType> findAll(){
		return ListUtil.toList(aftersaleHandleTypeDao.findAll());
	}
				
	public List<AftersaleHandleType> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(aftersaleHandleTypeDao.findAll(condition));
	}		

	public Page<AftersaleHandleType> findAll(
		String name,
		String remark,
		int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if(StringUtil.isNotEmpty(name)){
			condition.and($.name.contains(name));
		}
		if(StringUtil.isNotEmpty(remark)){
			condition.and($.remark.contains(remark));
		}
		/*
		Sort sortObject = null;
		if(StringUtil.isEmpty(sortAttribute)){
			sortObject = new Sort(Direction.DESC, "id");
		}else{
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
		}
		*/
		
		Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
		
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
		return aftersaleHandleTypeDao.findAll(condition, pageRequest);
	}
	
	public AftersaleHandleType getAftersaleHandleType(Long id){
		return aftersaleHandleTypeDao.getOne(id);
	}
	
	public void deleteAftersaleHandleType(Long id) throws CannotDelException{
		if (this.findAftersaleHandleType(id)) {
			throw new CannotDelException("该类型已经被售后记录引用，请先删除售后记录或修改售后记录的引用！");
		} else {
			this.aftersaleHandleTypeDao.delete(id);
		}
	}
	private boolean findAftersaleHandleType(Long aftersaleHandleTypeId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.aftersaleHandleType.id.eq(aftersaleHandleTypeId));
		return this.aftersaleRecordDao.count(condition) > 0;
	}

	public void saveAftersaleHandleType(AftersaleHandleType aftersaleHandleType) {
		if (null == aftersaleHandleType.getOrderNumber())
			aftersaleHandleType.setOrderNumber(this.getOrderNumber());
		
		aftersaleHandleTypeDao.save(aftersaleHandleType);
	}
	
	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<AftersaleHandleType> aftersaleHandleTypes = this.aftersaleHandleTypeDao.findAll(condition,pageRequest);
		if(aftersaleHandleTypes.getTotalElements() > 0){
			AftersaleHandleType aftersaleHandleType = aftersaleHandleTypes.getContent().get(0);
			return aftersaleHandleType.getOrderNumber()+1;
		}
		return 1;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		AftersaleHandleType currentModule = this.aftersaleHandleTypeDao.findOne(id);
		AftersaleHandleType targetModule = this.aftersaleHandleTypeDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.aftersaleHandleTypeDao.save(currentModule);
		this.aftersaleHandleTypeDao.save(targetModule);
		return true;
	}

}
		
